package repositories

import (
	"errors"
	"fox/db"
	"fox/models"
	request2 "fox/request"
	response2 "fox/response"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"math"
)

type UserRepository struct {
}

func NewUserRepository() (userRepository *UserRepository) {
	return &UserRepository{}
}

// Login TODO 测试
func (repository *UserRepository) Login(ctx *gin.Context) string {
	db.GDatabase.Db.Where("id = ?", 1).First(&models.User{})
	return "111"
}

// JsonRpc TODO 测试
func (repository *UserRepository) JsonRpc() string {
	return "jsonRpc"
}

// InsertUser 添加用户
func (repository *UserRepository) InsertUser(user *models.User) (id int64, err error) {
	var (
		result *gorm.DB
	)

	result = db.GDatabase.Db.Create(user)
	if result.RowsAffected == 1 {
		return user.ID, err
	}
	return 0, result.Error
}

// DeleteUserById 删除用户
func (repository *UserRepository) DeleteUserById(id int64) (err error) {
	var (
		result *gorm.DB
	)

	result = db.GDatabase.Db.Delete(&models.User{ID: id})
	if result.RowsAffected == 1 && result.Error == nil {
		return err
	}
	return result.Error
}

// UpdateUserById 修改用户信息
func (repository *UserRepository) UpdateUserById(user *models.User) (newUser *models.User, err error) {
	var (
		result *gorm.DB
	)

	result = db.GDatabase.Db.Model(&models.User{ID: user.ID}).UpdateColumns(user)
	result.Scan(&newUser)
	return newUser, result.Error

}

// SelectUserById 查找用户信息
func (repository *UserRepository) SelectUserById(c *gin.Context, id int64) (user *models.User, err error) {
	var (
		result *gorm.DB
	)

	result = db.GDatabase.Db.WithContext(c).Where("id = ?", id).First(&user)
	if result.RowsAffected == 1 {
		return user, nil
	}
	if result.RowsAffected == 0 {
		return nil, errors.New("没有查找到记录")
	}
	return user, result.Error
}

// SelectUsers 分页获取用户信息
func (repository *UserRepository) SelectUsers(user *request2.User) (result *response2.Result, err error) {
	var (
		offset    int
		users     *[]models.User
		countUser *models.User
		count     int64
		totalPage int
	)
	countUser = new(models.User)
	countUser.UserName = user.UserName
	count = repository.CountUsers(countUser)
	if count == 0 {
		result.Total = 0
		result.Size = user.Size
		result.Page = user.Page
		result.Lists = make([]interface{}, 0)
		return result, err
	}

	totalPage = int(math.Ceil(float64(count) / float64(user.Size)))
	offset = (user.Page - 1) * user.Size
	db.GDatabase.Db.Scopes(request2.Paginate(offset, user.Size), repository.filterUserUserName(user.UserName)).Model(&models.User{}).Order(user.SortField + " " + user.Sort).Find(&users)
	return &response2.Result{
		Total:     count,
		TotalPage: totalPage,
		Page:      user.Page,
		Size:      user.Size,
		Lists:     users,
	}, err
}

// CountUsers 统计分页数
func (repository *UserRepository) CountUsers(user *models.User) (count int64) {

	db.GDatabase.Db.Scopes(repository.filterUserUserName(user.UserName)).Model(&models.User{}).Count(&count)
	return count
}

// 增加where查询
func (repository *UserRepository) filterUserUserName(userName string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if userName != "" {
			return db.Where("username LIKE ?", "%"+userName+"%")
		}
		return db
	}
}

// 通过账号查询用户数据

func (repository *UserRepository) SelectUserByUsername(username string) (user *models.User, err error) {
	var (
		result *gorm.DB
	)

	result = db.GDatabase.Db.Where("username = ?", username).First(&user)
	if result.RowsAffected == 1 {
		return user, nil
	}
	if result.RowsAffected == 0 {
		return nil, errors.New("没有查找到记录")
	}
	return user, result.Error
}

// 通过token 查询用户全部信息

func (repository *UserRepository) SelectUserByToken(token string) (user *models.User, err error) {
	var (
		result *gorm.DB
	)

	result = db.GDatabase.Db.Where("auth_token = ?", token).First(&user)
	if result.RowsAffected == 1 {
		return user, nil
	}
	if result.RowsAffected == 0 {
		return nil, errors.New("没有查找到记录")
	}
	return user, result.Error
}
